package hms.RoomManagementModule;

import hms.database.DAO;
import hms.database.DAOnoGUI;
import hms.rooms.Room;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.LinkedList;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class ReservationStatusPanel extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private ReservationStatusPanel rPanel;
	private Room room;
	private ManageRoomsView view;

	protected JButton btn_ok;
	private JTable table;
	private DefaultTableModel model;
	
	/**
	 * Create the dialog.
	 */
	public ReservationStatusPanel(Room i_room, ManageRoomsView i_view) {
		rPanel = this;
		room = i_room;
		view = i_view;
		setAlwaysOnTop(true);
		setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
		setResizable(false);
		setTitle("Reservation Status");
		
		setBounds(100, 100, 820, 323);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JLabel lbl_roomLabel = new JLabel("Room: ");
		lbl_roomLabel.setFont(new Font("Roboto Cn", Font.PLAIN, 18));
		
		String labels[] = {"Customer", "Is Checked In", "Check-In Date", "Check-Out Date", "Reservation Fee"};
		model = new DefaultTableModel(new String[0][5], labels);
		
		table = new JTable(model);
		JScrollPane scrollPane = new JScrollPane(table);
		
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 784, Short.MAX_VALUE)
						.addComponent(lbl_roomLabel))
					.addContainerGap())
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lbl_roomLabel)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE))
		);
		
		
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				btn_ok = new JButton("OK");
				btn_ok.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						rPanel.dispose();
					}
				});
				btn_ok.setActionCommand("btn_ok");
				buttonPane.add(btn_ok);
				getRootPane().setDefaultButton(btn_ok);
			}
		}
		
		try {
			populateRoomReservations();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
	
	
	/**
	 * Populate the view with the information on reservations from the database
	 * @throws SQLException 
	 */
	private void populateRoomReservations() throws SQLException{
		DAO db = DAOnoGUI.getDAO();
		ResultSet roomIDSet = db.getResult("SELECT roomID FROM ROOM WHERE ROOMLABEL = '" + room.getRoomLabel() + "';");
		
		String roomID = "";
		
		while(roomIDSet.next()){
			roomID = roomIDSet.getString("roomID");
		}
		
		//System.out.println(roomID);
		
		
		ResultSet roomReservationIDs = db.getResult("SELECT RESERVATION_ID FROM ROOM_RESERVATION " +
				"WHERE ROOMID = " + roomID + ";");

		LinkedList<String> reservationIDs = new LinkedList<String>();
		while(roomReservationIDs.next()){
			reservationIDs.add(roomReservationIDs.getString("RESERVATION_ID"));
		}
		
		for(int i = 0; i < reservationIDs.size(); i++){
			ResultSet reservation = db.getResult("SELECT * FROM RESERVATION WHERE " +
					"RESERVATION_ID = " + reservationIDs.get(i) + ";");
			
			while(reservation.next()){
				Date checkInDate = reservation.getDate("CHECK_IN");
				Date checkOutDate = reservation.getDate("CHECK_OUT");
				Double reservationFee = reservation.getDouble("RESERVATION_FEE");
				boolean isCheckedIn = reservation.getBoolean("IS_CHECKED_IN");
				String guestName = "";
				String strCheckInDate = "";
				String strCheckOutDate = "";
				if(checkInDate != null)
					strCheckInDate = checkInDate.toString();
				if(checkOutDate != null)
					strCheckOutDate = checkOutDate.toString();
				
				ResultSet guestReservationIDSet = db.getResult("SELECT GUEST_ID FROM GUEST_RESERVATION WHERE " +
						"RESERVATION_ID = " + reservation.getInt("RESERVATION_ID") + ";");
				
				guestReservationIDSet.next();
				
				ResultSet guest = db.getResult("SELECT * FROM GUEST WHERE " +
						"GUEST_ID = " + guestReservationIDSet.getInt("GUEST_ID") + ";");
				
				guest.next();
				
				guestName = guest.getString("FIRST_NAME") + " " + guest.getString("LAST_NAME");
				
				model.insertRow(0, new Object[]{guestName, isCheckedIn, strCheckInDate, strCheckOutDate, reservationFee});
				
			}
		}
	}
}
